﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.IoC;
using Kooboo.Domain;
using Kooboo.Commerce.Contexts.Brands;
using Kooboo.Commerce.Contexts.Products;

namespace Kooboo.Commerce.Contexts.ProductTypes
{
    public static class EditorBehavior
    {
        #region ProductType
        public static void Post(this IEditor actor, IProductType productType)
        {
            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    var repository = ContextContainer.Current.Resolve<IProductTypeRepository>();
                    repository.Post(productType);
                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

        }

        public static void Put(this IEditor actor, IProductType productType)
        {

            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    var repository = ContextContainer.Current.Resolve<IProductTypeRepository>();
                    repository.Put(productType);
                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

        }

        public static void Delete(this IEditor actor, IProductType productType)
        {

            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {

                    var item = actor.QueryProductTypes().Where(i => i.Id == productType.Id).FirstOrDefault();

                    var brands = actor
                        .QueryBrands()
                        .Where(i => i.ProductTypes.Any(n => n.Id == item.Id))
                        .Select(i => i)
                        .AsEnumerable();

                    foreach (var brand in brands)
                    {
                        brand.Exclude(item);
                    }


                    foreach (var i in item.Products)
                    {
                        actor.Delete(i);
                    }

                    //item.Products.SelectMany(i => i.Orders)
                    //    .ToDelete()
                    //    .Execute();

                    item.CustomFields
                        .SelectMany(i => i.ValidationRules)
                        .ToDelete()
                        .Execute();

                    item.CustomFields
                      .ToDelete()
                      .Execute();

                    item.Variations
                        .SelectMany(i => i.ValidationRules)
                        .ToDelete()
                        .Execute();

                    item.Variations
                        .ToDelete()
                        .Execute();

                    var repository = ContextContainer.Current.Resolve<IProductTypeRepository>();
                    repository.Delete(item);


                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

        }

        public static void Delete(this IEditor actor, IEnumerable<IProductType> items)
        {
            foreach (var item in items)
            {
                actor.Delete(item);
            }
        } 
        #endregion
    }
}
